Skip to content

Conversation

@shsms
Copy link
Contributor

@shsms shsms commented Nov 20, 2025

  • Also implements the new Formula/Formula3Phase classes to replace the old formula engine, for supporting the new formula functions - max/min/coalesce.
  • Drops the old component graph and the old formula engine

@shsms shsms requested a review from a team as a code owner November 20, 2025 22:44
@shsms shsms requested review from Copilot and daniel-zullo-frequenz and removed request for a team November 20, 2025 22:44
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:data-pipeline Affects the data pipeline part:core Affects the SDK core components (data structures, etc.) part:microgrid Affects the interactions with the microgrid labels Nov 20, 2025
@shsms
Copy link
Contributor Author

shsms commented Nov 20, 2025

Based on #1283

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR switches from a Python-based component graph and formula engine to a new Rust-based implementation, introducing new Formula and Formula3Phase classes to support advanced formula functions (max/min/coalesce) and removing deprecated code.

Key Changes:

  • Replaced old Python formula engine with new Rust-based component graph
  • Implemented new Formula and Formula3Phase classes with support for max/min/coalesce functions
  • Updated component data structures to use new client library types
  • Removed deprecated formula engine tests and implementation

Reviewed Changes

Copilot reviewed 109 out of 118 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/timeseries/test_formula_engine.py Entire file deleted - old formula engine tests removed
tests/timeseries/test_consumer.py Added missing resampler calls for battery and PV inverter power
tests/timeseries/mock_resampler.py Updated to use new Metric enum instead of ComponentMetricId
tests/timeseries/mock_microgrid.py Updated component types and connection structures to match new API
tests/timeseries/_formulas/test_lexer.py New test file for formula lexer functionality
tests/timeseries/_formulas/test_formulas_3_phase.py New test file for three-phase formula support
tests/timeseries/_formulas/test_formulas.py New comprehensive formula tests with max/min/coalesce support
tests/timeseries/_formulas/test_formula_composition.py Updated to use new formula API and async context managers
src/frequenz/sdk/timeseries/formulas/_token.py New token definitions for formula parsing
src/frequenz/sdk/timeseries/formulas/_resampled_stream_fetcher.py New component for fetching resampled telemetry streams
src/frequenz/sdk/timeseries/formulas/_peekable.py New peekable iterator implementation for parsing
src/frequenz/sdk/timeseries/formulas/_parser.py New formula parser implementation
src/frequenz/sdk/timeseries/formulas/_lexer.py New formula lexer implementation
src/frequenz/sdk/timeseries/formulas/_functions.py New function implementations (max/min/coalesce)
Comments suppressed due to low confidence (1)

tests/timeseries/mock_microgrid.py:9

  • The import Callable from collections.abc on line 10 is unused after the changes. The Callable type is imported again on line 12 from the typing module via the Coroutine import, and all usages in the file appear to use the typing module's Callable (e.g., line 260). This unused import should be removed.
from collections.abc import Callable

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@shsms shsms force-pushed the new-formulas+new-graph= branch from 4583c7d to c942e5f Compare November 20, 2025 22:49
@llucax
Copy link
Contributor

llucax commented Nov 21, 2025

Can you rebase it on top of Based on #1293 instead?

@shsms
Copy link
Contributor Author

shsms commented Nov 25, 2025

Can you rebase it on top of Based on #1293 instead?

#1293 is making changes to a lot of the files that are being deleted by this PR. I'd rather take this in first, then drop the changes on these files, then we only need to review required changes.

@shsms
Copy link
Contributor Author

shsms commented Nov 25, 2025

Oh, maybe those changes are from the api PR. don't know. Let's take the API PR first then, we can decide.

@shsms shsms force-pushed the new-formulas+new-graph= branch 2 times, most recently from 706118b to cc1e61b Compare November 26, 2025 17:19
@shsms
Copy link
Contributor Author

shsms commented Nov 26, 2025

It was actually trivial to rebase on top of #1293. Done.

@shsms shsms force-pushed the new-formulas+new-graph= branch from cc1e61b to 09b029b Compare November 26, 2025 17:29
@shsms
Copy link
Contributor Author

shsms commented Nov 26, 2025

Damn, this causes my tests to fail. Switched back to based on #1283.

Since we are taking several `timedelta`s, it could be a bit confusing on
the call site to know which is which, so better to require to use them
as keyword arguments.

Signed-off-by: Leandro Lucarella <[email protected]>
After some refactoring, result can't be `None` anymore, so we don't need
to consider that case.

Signed-off-by: Leandro Lucarella <[email protected]>
Import from `collections.abc` instead, and also use a direct import of
`cast` instead of using it from `typing`.

Signed-off-by: Leandro Lucarella <[email protected]>
The _Mocks class already provides a `.stop()` methods to cleanup, so use
that to avoid duplicated code.

Signed-off-by: Leandro Lucarella <[email protected]>
This is useful when debugging tests, to get a nice representation of the
component graph.

Signed-off-by: Leandro Lucarella <[email protected]>
Type stubs are now [officially available][1], so we use them instead of
instruct `mypy` to ignore type hints for the `networkx` module.

[1]: networkx/networkx#3988 (comment)

Signed-off-by: Leandro Lucarella <[email protected]>
This makes the terminology more precise, avoids confusion, and matches
the new microgrid API naming.

Signed-off-by: Leandro Lucarella <[email protected]>
Use the URL for the v0.18 sandbox.

This is a WIP because it actually points to the v0.17 sandbox, as there
is no v0.18 sandbox yet.

Signed-off-by: Leandro Lucarella <[email protected]>
@shsms shsms force-pushed the new-formulas+new-graph= branch 2 times, most recently from 49e8ff8 to c88d813 Compare November 28, 2025 13:48
shsms added 22 commits November 28, 2025 14:51
It sends requests to the resampler and fetches resampled telemetry
streams.

Signed-off-by: Sahas Subramanian <[email protected]>
Waits for new values from the input data streams.  When there's one
new value from each of them, evaluates the AST and sends the
calculated value out.

Signed-off-by: Sahas Subramanian <[email protected]>
This is a wrapper around the FormulaEvaluatingActor, with methods for
composing multiple formulas.

Signed-off-by: Sahas Subramanian <[email protected]>
Signed-off-by: Sahas Subramanian <[email protected]>
This also adds an evaluator and tests for the 3-phase formulas.

Signed-off-by: Sahas Subramanian <[email protected]>
This will replace the SDK's old component graph and the formula
generators.

This commit also adds some graph traversal methods that are not
provided by the component graph library yet.

Signed-off-by: Sahas Subramanian <[email protected]>
Graphs in island mode are not supported yet.  Earlier, this test was
using an `Unspecified` component category as a junction node, to start
traversing from, which is inaccurate.

The `Unspecified` component should always be an error, to identify an
unfilled category field, due to a bug.

Signed-off-by: Sahas Subramanian <[email protected]>
Signed-off-by: Sahas Subramanian <[email protected]>
This won't be necessary as soon as the coalesce AST node is able to subscribe
to secondary streams only if primary streams are not available.

But for now, coalesce nodes need all their input streams to have data.

Signed-off-by: Sahas Subramanian <[email protected]>
The new formulas use component metrics as primary and only when those
are not available, uses any corresponding meters as fallback.

Signed-off-by: Sahas Subramanian <[email protected]>
The new formulas use the CHP meter in the grid formula, and in the CHP
formula, both of which are in different namespaces.

This change makes the same data available from two separate streams
from the mock_resampler.

Signed-off-by: Sahas Subramanian <[email protected]>
Signed-off-by: Sahas Subramanian <[email protected]>
@shsms shsms force-pushed the new-formulas+new-graph= branch from c88d813 to 4f25d12 Compare November 28, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:core Affects the SDK core components (data structures, etc.) part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:microgrid Affects the interactions with the microgrid part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.)

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

2 participants